package com.example.benzinrechner;

/**
 * Created by elizaveta on 15/12/13.
 */

import android.content.ContentValues;
import android.database.sqlite.*;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.nio.channels.FileChannel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.widget.Toast;


public class SaveResults extends SQLiteOpenHelper {


    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "spritInfo";
    private static final String KEY_KOSTEN = "kosten";
    private static final String KEY_LITER ="liter";
    private static final String KEY_LITERPREIS ="literpreis";
    private static final String DATABASE_NAME = "spritMan.db";
    float result;


    SaveResults(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_KOSTEN + " FLOAT," + KEY_LITER + " FLOAT,"
                + KEY_LITERPREIS + " FLOAT" + ")";
        db.execSQL(CREATE_TABLE);
    }



    @Override
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){

        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
        this.onCreate(db);
    }

    public void addResults(float kosten, float liter, float literpreis){

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_KOSTEN, kosten);
        values.put(KEY_LITER, liter);
        values.put(KEY_LITERPREIS, literpreis);

        db.insert("spritInfo", null, values);
        db.close();
    }

    public float getResults(int index){

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                result = cursor.getFloat(index);


            } while (cursor.moveToNext());
        }

        return result;
    }



}
